<script name="CallbackBind" lang="ts" setup>
import { ref } from 'vue'
import { QqUserInfo } from '@/types/user'
import { useForm, useField } from 'vee-validate'
import { mobileRule, codeRule } from '@/utils/validate'
import useStore from '@/store'
import { useCountDown } from '@/hooks'
const { userStore } = useStore()
const userInfo = ref<QqUserInfo>({} as QqUserInfo)
let openId = ''
if (QC.Login.check()) {
  // 通过QC调用get_user_info这个接口
  QC.api('get_user_info').success((resp: any) => {
    userInfo.value = resp.data
  })
  // openid
  QC.Login.getMe((id: string) => {
    openId = id
  })
}

// 表单功能
const { validate } = useForm({
  validationSchema: {
    mobile: mobileRule,
    code: codeRule
  }
})

const {
  value: mobile,
  errorMessage: mobileErrMsg,
  validate: mobileValidate
} = useField<string>('mobile')
const { value: code, errorMessage: codeErrMsg } = useField('code')

// 发送验证码
const { time, start } = useCountDown(60)
const send = async () => {
  if (time.value > 0) return
  const { valid } = await mobileValidate()
  if (!valid) return
  await userStore.sendQQBindMsg(mobile.value)
  start()
}

// 绑定
const bind = async () => {
  const { valid } = await validate()
  if (!valid) return
  userStore.qqBindLogin(openId, mobile.value, code.value)
}
</script>
<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="userInfo.figureurl_qq_2" alt="" />
      <p>
        Hi，{{ userInfo.nickname }}
        欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~
      </p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          v-model="mobile"
          class="input"
          type="text"
          placeholder="绑定的手机号"
        />
      </div>
      <div class="error">{{ mobileErrMsg }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          v-model="code"
          class="input"
          type="text"
          placeholder="短信验证码"
        />
        <span class="code" @click="send">
          {{ time ? time + 'S' : '发送验证码' }}
        </span>
      </div>
      <div class="error">{{ codeErrMsg }}</div>
    </div>
    <a href="javascript:;" class="submit" @click="bind">立即绑定</a>
  </div>
</template>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
